﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace ActiveXTree
{
    public class DrawTree:Panel
    {
        private Graphics _graphics;
        private int dy = 50;
        Brush b = new HatchBrush(HatchStyle.Percent05, Color.Black);
        Brush c = new HatchBrush(HatchStyle.Percent90, Color.Red);
        Pen pen = new Pen(Color.FromArgb(255, 8, 6, 9), 1);
        public void Draw(treeNode _root)
        {
            Width = 1024;
            Height = 768;
            BackgroundImage = new Bitmap(Width, Height);
            _graphics = Graphics.FromImage(BackgroundImage);
            showTree(_root, Width / 2, 0, Width / 2, Width / 2);
        }
        void showTree(treeNode p, int x, int y, int xp, int dx)
        {
            if (p == null) return;
            _graphics.DrawLine(pen, x, y, xp, y + dy);
            _graphics.FillRectangle(b, xp - 20, y + dy, 40, 40);
            _graphics.DrawString(p.value.ToString(), new Font("Microsoft Sans Serif", 14), c, xp - 10, y + 12 + dy);
            showTree(p.left, xp, y + dy, xp - dx / 2, dx / 2);
            showTree(p.right, xp, y + dy, xp + dx / 2, dx / 2);
        }
    }
}
